<template>
  <div>
    <a-card style='margin-top: 50px;'>
      <a-divider orientation='left'>定时任务但是手动执行</a-divider>
      <a-space>
        <a-popover title="执行任务内容">
          <template slot="content">
            <p>将每个新人用户的作答分配给判题人</p>
            <p>分配成功后方可评分</p>
            <p>执行时间: <a-tag color='red'>答题结束后</a-tag></p>
          </template>
          <a-button @click='showModal("allot")'>新人分配</a-button>
        </a-popover>
  
        <a-popover title="执行任务内容">
          <template slot="content">
            <p>将创建时间超过30天的图床文件删除</p>
            <p>执行时间: <a-tag color='red'>随便</a-tag></p>
          </template>
          <a-button @click='showModal("clear")'>清理文件</a-button>
        </a-popover>
  
        <a-popover title="执行任务内容">
          <template slot="content">
            <p>将注册后未作答的用户总分设置为0</p>
            <p>执行时间: <a-tag color='red'>答题结束后</a-tag></p>
          </template>
          <a-button @click='showModal("zero")'>自动判零</a-button>
        </a-popover>
  
        <a-popover title="执行任务内容">
          <template slot="content">
            <p>将协会内用户的作答记录全部清除</p>
            <p>执行时间: <a-tag color='red'>随便</a-tag></p>
          </template>
          <a-button @click='showModal("reply")'>删除记录</a-button>
        </a-popover>
  
        <a-popover title="执行任务内容">
          <template slot="content">
            <p>将低于分数线的用户 设置录取未通过状态</p>
            <p>将高于分数线的用户 设置录取通过状态</p>
            <p>执行时间: <a-tag color='red'>!!!所有评分结束后!!!</a-tag></p>
          </template>
          <a-button @click='showModal("status")'>设置状态</a-button>
        </a-popover>
      </a-space>
      <a-divider></a-divider>
    </a-card>
    
    <a-modal
      title='定时任务手动执行'
      :visible='visible'
      okText='开始执行'
      cancelText='取消执行'
      :confirm-loading='confirmLoading'
      @ok='handleOk'
      @cancel='handleCancel'
    >
      <a-form
        :label-col='{ span: 6 }'
        :form='form'
        :wrapper-col='{ span: 15 }'
        formLayout='horizontal'>
        
        <a-form-item label='assessKey'>
          <a-input-password
            v-decorator="['assessKey',{rules: [{ required: true, message: 'assessKey不能为空' }]}]"
            placeholder='请输入assessKey'>
            <a-icon slot='prefix' type='lock' :style="{ color: 'rgba(0,0,0,.25)' }" />
          </a-input-password>
        </a-form-item>
        
        <a-form-item label='secretKey'>
          <a-input-password
            v-decorator="['secretKey',{rules: [{ required: true, message: 'secretKey不能为空' }]}]"
            placeholder='请输入secretKey'>
            <a-icon slot='prefix' type='lock' :style="{ color: 'rgba(0,0,0,.25)' }" />
          </a-input-password>
        </a-form-item>
      
      </a-form>
    </a-modal>
  
  </div>
</template>

<script>

import { allotNewcomerJob, calcTotalScoreJob, deleteAdminReplys, fileClearJob, setStatus } from '@/api/job'

export default {
  name: 'Job',
  data() {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this, { name: 'job' }),
      type: ''
    }
  },
  created() {
  },
  methods: {
    showModal(type) {
      this.type = type
      this.visible = true
    },
    handleOk(e) {
      const hide = this.$message.loading('任务执行中...', 0);
      this.confirmLoading = true
      e.preventDefault()
      
      this.form.validateFields((err, values) => {
        if (!err) {
          if (this.type === 'allot') {
            // 分配新人
            allotNewcomerJob(values).then(() => {
              this.$message.success('分配任务执行成功')
              this.visible = false
            })
          } else if (this.type === 'file') {
            // 文件清理
            fileClearJob(values).then(() => {
              this.$message.success('文件清理任务执行成功')
              this.visible = false
            })
          } else if (this.type === 'zero') {
            // 计算分数
            calcTotalScoreJob(values).then(() => {
              this.$message.success('计算分数任务执行成功')
              this.visible = false
            })
          } else if (this.type === 'reply') {
            deleteAdminReplys(values).then(() => {
              this.$message.success('清除协会内用户作答记录任务执行成功')
              this.visible = false
            })
          } else if (this.type === 'status') {
            setStatus(values).then(() => {
              this.$message.success('所有状态均设置成功')
              this.visible = false
            })
          }
        }
      })
      setTimeout(hide, 1000);
      this.confirmLoading = false
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>